/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs575.coresystem;

import cs575.dbentities.User;
import cs575.dbentities.Task;
import cs575.dbentities.GroupEx;
import cs575.dbentities.TaskAssign;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author bms
 */
public class TaskManagerE{
    
    private EntityManager em;

    public TaskManagerE() {
        this.em = null;
    }

    public TaskManagerE(EntityManager em) {
        this.em = em;
    }     
    
    public Task createTask(String taskName, String taskDescription, String dueDate, GroupEx group) throws DBStoringException {
        
        EntityTransaction trans = em.getTransaction();
        trans.begin();
        
        Task newTask = new Task(taskName, taskDescription, dueDate, 1, group);
        //System.out.println(newTask.getTaskId());
        if(storeTask(newTask)){
            trans.commit();
            return newTask;
        }
        else{
            trans.rollback();
            return null;
        }  
    }

    /**
     * 
     * @param task - Task
     * @return 
     */
    public boolean storeTask(Task task) throws DBStoringException {
        boolean result = false;
        
        try {          
            em.persist(task);
            result = true;
        } catch (Exception e){
            throw new DBStoringException("storeTask: Failed!");
        }

        return result;
    }    
    

   /**
     * 
     * @param task - Task
     * @return 
     */
    public boolean storeTaskAssignment(TaskAssign ta) throws DBStoringException {
        boolean result = false;
        
        try {          
            em.persist(ta);
            result = true;
        } catch (Exception e){
            throw new DBStoringException("storeTaskAssignment: Failed!");
        }

        return result;
    }     
    
    
    /**
     * 
     * @param taskId
     * @return 
     */
    public Task retrieveTask(int taskId)
    {
        Task task = em.find(Task.class, taskId);
        System.out.println("retrieveTask:" + task.getTaskName());
        return task;
    }
    
    
    /**
     * 
     * @param task
     * @return Boolean
     * @throws DBStoringException 
     */
    public boolean editTask(Task task) throws DBStoringException {
        boolean result = false;
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            Task taskX = em.find(Task.class, task.getTaskId());
            taskX.setTaskName(task.getTaskName());
            taskX.setTaskDescription(task.getTaskDescription());
            taskX.setDueDate(task.getDueDate());
            if(storeTask(taskX)){
                result = true;
            }
        } catch (Exception e){
            throw new DBStoringException("editTask: Failed!" + task.toString());
        } finally{
            if(result){
                trans.commit();
            }else{
                trans.rollback();
            }
        }

        return result;
    }	

        
    /**
     * This methods loads and removes a given task based on the task id
     * 
     * @param task
     * @return
     * @throws DBStoringException 
     */
    public boolean deleteTask(Task task) throws DBStoringException {
        boolean result = false;
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            // Load the task first
            Task newTask = em.getReference(Task.class, task.getTaskId());
            //Task newTask = this.retrieveTask(taskId);
            // Delete the entity
            em.remove(newTask);
            result = true;
        } catch (Exception e){
            throw new DBStoringException("deleteTask: Failed!");
        } finally {
            if(result){
                trans.commit();
            }else{
                trans.rollback();
            }
        }
        return result;
    }	
    

    /**
     * 
     * @param task
     * @param soucUser
     * @param destUser
     * @return Boolean
     * @throws DBStoringException 
     */
    public boolean assignTask(Task task, User soucUser, User destUser) throws DBStoringException {
        boolean result = false;
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            TaskAssign ta = new TaskAssign();
            ta.setSourceUser(soucUser);
            ta.setDestUser(destUser);
            ta.setTask(task);
            if(storeTaskAssignment(ta)){
                result = true;
            }
        } catch (Exception e){
            throw new DBStoringException("assignTask: Failed!");
        } finally {
            if(result){
                trans.commit();
            }else{
                trans.rollback();
            }
        }
         
        return result;
    }	

    
    /**
     * 
     * @param task
     * @param user
     * @return
     * @throws DBStoringException 
     */
    public boolean completeTask(Task task, User user) throws DBStoringException {
        boolean result = false;
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            task.setStatus(task.Completed);
            if(storeTask(task)){
                result = true;
            }
        } catch (Exception e){
            throw new DBStoringException("assignTask: Failed!");
        } finally {
            if(result){
                trans.commit();
            }else{
                trans.rollback();
            }
        }
         
        return result;
    }        
}